home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / conhlp03 / padp_src / getbuff.c < prev    next >
C/C++ Source or Header  |  1995-02-18  |  941b  |  51 lines

  1. /*
  2.     getbuff.c
  3.     94/11/27 94/11/28 95/02/18
  4.     ret : 0 : normal
  5.           1 : eof
  6.           2 : memory full
  7.           3 : buffur full
  8. */
  9.  
  10. #include <malloc.h>
  11. #include <io.h>
  12.  
  13. #define byte unsigned char
  14. #define uint unsigned int
  15.  
  16. #define BUFFSIZE ( 1024 * 2 )
  17. #define BUFPAGE 20
  18.  
  19. static byte *mp[BUFPAGE];
  20. static int mpg = -1;
  21. static uint mp_buf = BUFFSIZE;
  22.  
  23. byte getbuff( int fp, uint offset, byte *c ){
  24.  
  25.     int cpg,cby;
  26.     int flag = NULL;
  27.  
  28.     cpg = offset / BUFFSIZE;
  29.     cby = offset % BUFFSIZE;
  30.     while( cpg > mpg && mpg < BUFPAGE && mp_buf == BUFFSIZE ){
  31.     /* バッファメモリの確保 */
  32.         if( ++mpg > BUFPAGE ){
  33.             flag = 2 ;
  34.             break;
  35.         }
  36.         if(( mp[mpg] = (byte *)malloc(BUFFSIZE) ) == NULL ){
  37.             flag = 3 ;
  38.             break;
  39.         }
  40.     /* 読み込み */
  41.         mp_buf = read(fp,mp[mpg],BUFFSIZE);
  42.     }
  43.     if( mp_buf <= cby && cpg == mpg ) flag = 1 ;
  44.     if( flag != NULL ){
  45.         *c = NULL ;
  46.     } else {
  47.         *c = *(mp[cpg]+cby);
  48.     }
  49.     return flag;
  50. }
  51.